//
//  ImageAnnView.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/18.
//  Copyright © 2018年 mmda. All rights reserved.
//

//import MapKit
//import SnapKit

class ImageAnnView: BMKAnnotationView {
    
    var imageView: UIImageView!
    var titleLabel: UILabel!
    
    var videoTagImageView: UIImageView!
    
    
    override init!(annotation: BMKAnnotation!, reuseIdentifier: String!) {
        
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        
//        40 * 48
        self.image = #imageLiteral(resourceName: "annotation")
        
        imageView = UIImageView(frame: CGRect(x: 1, y: 1, width: 38, height: 38))
        
        imageView.layer.cornerRadius = 19
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFill
        imageView.image = #imageLiteral(resourceName: "default")
        
        self.addSubview(imageView)
        
        videoTagImageView = UIImageView(image: #imageLiteral(resourceName: "video"))
        
        videoTagImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
        imageView.addSubview(videoTagImageView)
        videoTagImageView.isHidden = true

        
        titleLabel = UILabel(frame: CGRect(x: -5, y: 50, width: 50, height: 12))
        titleLabel.font = kFont10
        titleLabel.textColor = .white
        titleLabel.backgroundColor = UIColor(white: 0, alpha: 0.6)
        titleLabel.layer.cornerRadius = 6
        titleLabel.textAlignment = .center
        titleLabel.clipsToBounds = true
        
        self.addSubview(titleLabel)
        
//        self.enabled3D = true
//        self.isEnabled = true
    }
    
    
    
    func setup(with annotation:Annotation?) {
        
        self.annotation = annotation
        guard let ann = annotation else { return }
        
        if let mode = ann.model as? Blog {
            
            if let video = mode.videos?.first {
                
                imageView.setImage(string: video.video_cover, placeholder: #imageLiteral(resourceName: "placeholder"))
                
                self.videoTagImageView.isHidden = false
            } else {
                
                self.imageView.setImage(string: mode.pictures_new?.first?.getQiniuThumb(thumbImageWidth), placeholder: #imageLiteral(resourceName: "default"))
                self.videoTagImageView.isHidden = true
            }
            
            
            
            self.titleLabel.isHidden = true
            return
            
        }
        
        if let mode = ann.model as? User {
            
            self.imageView.setImage(string: mode.logo_new?.getQiniuThumb(thumbImageWidth), placeholder: #imageLiteral(resourceName: "default"))
            self.videoTagImageView.isHidden = true
            
            guard let name = mode.nickname else {
                
                self.titleLabel.isHidden = true
                return
            }
            
            self.titleLabel.isHidden = false
            self.titleLabel.text = name
            
            let titleWidth = name.getWidth(font: titleLabel.font) + 12
            
            self.titleLabel.frame = CGRect(x: -(titleWidth - 40) / 2, y: 50, width: titleWidth, height: 12)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

